<template>
  <div class="app-container">
    <el-button v-waves type="primary" @click="handleAdd">添加职位</el-button>

    <el-table
      :data="table.list"
      row-key = "roleId"
      v-loading="table.listLoading"
      style="width: 100%;margin-top:30px;"
      border
    >
      <el-table-column align="center" label="职位名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.positionName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="职位代码" width="220">
        <template slot-scope="scope">
          {{ scope.row.positionCode }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="职位等级" width="220">
        <template slot-scope="scope">
          {{ scope.row.positionLevel }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="职位说明">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column align="center" >
        <template slot-scope="scope">
          <el-button v-waves type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button v-waves type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialog.visible" :title="dialog.title">
      <el-form :model="position" label-width="80px" label-position="left" style="margin: 0px 20px 0px 20px">
        <el-form-item label="职位名称">
          <el-input v-model="position.positionName" placeholder="职位名称" />
        </el-form-item>
        <el-form-item label="职位代码">
          <el-input v-model="position.positionCode" placeholder="职位代码" />
        </el-form-item>
        <el-form-item label="职位等级">
          <el-input v-model="position.positionLevel" placeholder="职位等级" />
        </el-form-item>
        <el-form-item label="职位说明">
          <el-input
            v-model="position.description"
            :autosize="{ minRows: 2, maxRows: 4}"
            type="textarea"
            placeholder="职位说明"
          />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialog.visible=false">取消</el-button>
        <el-button type="primary" @click="confirmSave">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import { deepClone } from '@/utils'
import { list, info, addPost, editPost, disablePost } from '@/api/position'
const defaultPosition = {
  positionId: '',
  positionName: '',
  positionCode: '',
  positionLevel: '',
  description: ''
}

export default {
  directives: { waves },
  data() {
    return {
      table: {
        total: 0,
        list: [],
        listLoading: true
      },
      dialog: {
        visible: false,
        title: '',
        addTitle: '添加职位',
        editTitle: '编辑职位',
        type: '',
        addType: 'add',
        editType: 'edit'
      },
      position: defaultPosition
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    this.getTableList()
  },
  methods: {
    async getTableList() { // 得到职位列表
      this.table.listLoading = true
      this.table.list = await list()
      this.table.listLoading = false
    },
    handleAdd() { // 执行添加职位
      this.position = deepClone(defaultPosition)
      this.dialog.type = this.dialog.addType
      this.dialog.title = this.dialog.addTitle
      this.dialog.visible = true // open dialog
    },
    async handleEdit(scope) { // 执行修改职位
      this.dialog.type = this.dialog.editType
      this.dialog.title = this.dialog.editTitle
      this.dialog.visible = true // open dialog
      const positionId = scope.row.positionId // get positionId
      this.position = await info(positionId)
    },
    handleDelete({ $index, row }) { // 执行删除职位
      this.$confirm('确认删除该职位 ?', 'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await disablePost(row.positionId)
          this.table.list.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmSave() { // 提交新增或修改
      let notifyTitle = ''
      if (this.dialog.type === this.dialog.addType) {
        notifyTitle = '添加成功'
        await addPost(this.position)
      }
      if (this.dialog.type === this.dialog.editType) {
        notifyTitle = '修改成功'
        await editPost(this.position)
      }
      const { positionName, positionCode } = this.position
      const notify = { // notify
        title: `${notifyTitle}`,
        type: 'success',
        dangerouslyUseHTMLString: true,
        message: `<div>职位名称: ${positionName}</div> <div>职位代码: ${positionCode}</div>`
      }
      this.$notify(notify)
      this.dialog.visible = false// close dialog
      this.getTableList() // reload table
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
